package com.diqizu.servlet;

import java.io.IOException;

import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.diqizu.dao.NewsDaoImpl;
import com.diqizu.dao.NewsTypeDaoImpl;
import com.diqizu.entity.News;
import com.diqizu.entity.NewsType;





/**处理新闻相关的请求
 * Servlet implementation class newsServlet
 */
@WebServlet("/newsServlet")
public class newsServlet extends BaseServlet {

	/**
	 * 跳转到新闻页面
	 * 地址： localhost:8080/DiQiZu/newsServlet?method=toNews
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 * 
	 */
	public void toNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
			// 调用dao层的方法1.新闻类型列表2.新闻列表
			NewsDaoImpl newsDao = new NewsDaoImpl();
			ArrayList<News> listLimit6 = newsDao.getListLimit6();

			NewsTypeDaoImpl typeDao = new NewsTypeDaoImpl();
			ArrayList<NewsType> allList = typeDao.getAllList();

			req.setAttribute("newsList", listLimit6);//新闻列表
			req.setAttribute("newsTypeList", allList);//新闻类型列表

			req.getRequestDispatcher("article_list.jsp").forward(req, resp);

		}

		/**
		 * 根据类型id，获取新闻 当点击超链接时调用
		 * 
		 * @param req
		 * @param resp
		 * @throws ServletException
		 * @throws IOException
		 */
		public void getnewstype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

			String typeid = req.getParameter("typeid");
			// 调用dao层的方法
			// 2.新闻列表
			NewsDaoImpl newsDao = new NewsDaoImpl();
			ArrayList<News> ListByTypeId = newsDao.getListByTypeId(typeid);
			System.out.println("执行的语句是"+ListByTypeId);
			// 1.新闻类型列表
			NewsTypeDaoImpl typeDao = new NewsTypeDaoImpl();
			ArrayList<NewsType> allList = typeDao.getAllList();
			//根据新闻类型id查新闻
			req.setAttribute("newsList", ListByTypeId);
			req.setAttribute("newsTypeList", allList);
	        //跳转
			req.getRequestDispatcher("article_list.jsp").forward(req, resp);

		}

		

}

